﻿namespace KeRui.DAL
{
    using KeRui.DBUtility;
    using KeRui.Model;
    using System;
    using System.Data;
    using System.Data.SqlClient;

    public class MobileVisit
    {
        public static int Add(KeRui.Model.MobileVisit model)
        {
            int num;
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@ID", SqlDbType.Int, 4), new SqlParameter("@Accept", SqlDbType.VarChar, 450), new SqlParameter("@UserAgent", SqlDbType.VarChar, 450), new SqlParameter("@SystemCheck", SqlDbType.Int, 4), new SqlParameter("@UserCheck", SqlDbType.Int, 4), new SqlParameter("@CreateTime", SqlDbType.DateTime), new SqlParameter("@CreateIP", SqlDbType.VarChar, 15) };
            parameters[0].Direction = ParameterDirection.Output;
            parameters[1].Value = model.Accept;
            parameters[2].Value = model.UserAgent;
            parameters[3].Value = model.SystemCheck;
            parameters[4].Value = model.UserCheck;
            parameters[5].Value = model.CreateTime;
            parameters[6].Value = model.CreateIP;
            DbHelperSQL.RunProcedure("MobileVisit_ADD", parameters, out num);
            return (int) parameters[0].Value;
        }

        public void Delete(int ID)
        {
            int num;
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@ID", SqlDbType.Int, 4) };
            parameters[0].Value = ID;
            DbHelperSQL.RunProcedure("MobileVisit_Delete", parameters, out num);
        }

        public bool Exists(int ID)
        {
            int num;
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@ID", SqlDbType.Int, 4) };
            parameters[0].Value = ID;
            return (DbHelperSQL.RunProcedure("MobileVisit_Exists", parameters, out num) == 1);
        }

        public KeRui.Model.MobileVisit GetModel(int ID)
        {
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@ID", SqlDbType.Int, 4) };
            parameters[0].Value = ID;
            KeRui.Model.MobileVisit visit = new KeRui.Model.MobileVisit();
            DataSet set = DbHelperSQL.RunProcedure("MobileVisit_GetModel", parameters, "ds");
            if (set.Tables[0].Rows.Count > 0)
            {
                if (set.Tables[0].Rows[0]["ID"].ToString() != "")
                {
                    visit.ID = int.Parse(set.Tables[0].Rows[0]["ID"].ToString());
                }
                visit.Accept = set.Tables[0].Rows[0]["Accept"].ToString();
                visit.UserAgent = set.Tables[0].Rows[0]["UserAgent"].ToString();
                if (set.Tables[0].Rows[0]["SystemCheck"].ToString() != "")
                {
                    visit.SystemCheck = int.Parse(set.Tables[0].Rows[0]["SystemCheck"].ToString());
                }
                if (set.Tables[0].Rows[0]["UserCheck"].ToString() != "")
                {
                    visit.UserCheck = int.Parse(set.Tables[0].Rows[0]["UserCheck"].ToString());
                }
                if (set.Tables[0].Rows[0]["CreateTime"].ToString() != "")
                {
                    visit.CreateTime = DateTime.Parse(set.Tables[0].Rows[0]["CreateTime"].ToString());
                }
                visit.CreateIP = set.Tables[0].Rows[0]["CreateIP"].ToString();
                return visit;
            }
            return null;
        }

        public void Update(KeRui.Model.MobileVisit model)
        {
            int num;
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@ID", SqlDbType.Int, 4), new SqlParameter("@Accept", SqlDbType.VarChar, 450), new SqlParameter("@UserAgent", SqlDbType.VarChar, 450), new SqlParameter("@SystemCheck", SqlDbType.Int, 4), new SqlParameter("@UserCheck", SqlDbType.Int, 4), new SqlParameter("@CreateTime", SqlDbType.DateTime), new SqlParameter("@CreateIP", SqlDbType.VarChar, 15) };
            parameters[0].Value = model.ID;
            parameters[1].Value = model.Accept;
            parameters[2].Value = model.UserAgent;
            parameters[3].Value = model.SystemCheck;
            parameters[4].Value = model.UserCheck;
            parameters[5].Value = model.CreateTime;
            parameters[6].Value = model.CreateIP;
            DbHelperSQL.RunProcedure("MobileVisit_Update", parameters, out num);
        }
    }
}

